<template>
  <div id="app">

    <mt-header fixed title="道钉智能地锁"></mt-header>

    <div>
      <mt-radio title="选择控制的锁" v-model="lockSelValue" :options="seleLockVal">

      </mt-radio>

      <mt-button class="mint-button mint-button--primary mint-button--large" type="primary" @click="upClick">升锁</mt-button>
      <br>
      <br>
      <mt-button class="mint-button mint-button--danger mint-button--large" type="danger" @click="downClick">降锁</mt-button>
      <br>
      <br>
      <mt-button class="mint-button mint-button--default mint-button--large" type="default" @click="lockStatus">地锁状态</mt-button>

    </div>

  </div>
</template>

<script>
import { Toast, MessageBox } from 'mint-ui'

export default {
  data() {
    return {
      name: 'App',
      lockSelValue: '',
      popupVisible: true,
      seleLockVal: ['1号锁', '2号锁', '3号锁']
    }
  },
  methods: {
    upClick() {
      if (this.lockSelValue === '') {
        Toast('请选择车锁')
        return
      }

      var lCode = ''

      if (this.lockSelValue === this.seleLockVal[0]) {
        lCode = 'lock1'
      }
      if (this.lockSelValue === this.seleLockVal[1]) {
        lCode = 'lock1'
      }
      if (this.lockSelValue === this.seleLockVal[2]) {
        lCode = 'lock1'
      }
      this.dataUp(lCode, 1)
    },
    downClick() {
      if (this.lockSelValue === '') {
        Toast('请选择车锁')
        return
      }

      var lCode = ''

      if (this.lockSelValue === this.seleLockVal[0]) {
        lCode = 'lock1'
      }
      if (this.lockSelValue === this.seleLockVal[1]) {
        lCode = 'lock1'
      }
      if (this.lockSelValue === this.seleLockVal[2]) {
        lCode = 'lock1'
      }

      this.dataUp(lCode, 0)
    },
    lockStatus() {
      if (this.lockSelValue === '') {
        Toast('请选择车锁')
        return
      }

      var lCode = ''

      if (this.lockSelValue === this.seleLockVal[0]) {
        lCode = 'lock1'
      }
      if (this.lockSelValue === this.seleLockVal[1]) {
        lCode = 'lock1'
      }
      if (this.lockSelValue === this.seleLockVal[2]) {
        lCode = 'lock1'
      }

      axios
        .get(
          'https://test.daodingtech.com/xdrpark-app/V1/nblot/info?lockCode=' +
            lCode
        )
        .then(function(response) {
          var lockSt = ''

          if (response.data.errorCode != 200) {
            Toast('请求失败：' + response.data.errorMsg)
            return
          }

          if (response.data.data.lockStatus.slice(6,8) === '10') {
            lockSt = '已降位'
          }

          if (response.data.data.lockStatus.slice(6,8) === '01') {
            lockSt = '已升起'
          }

          MessageBox(
            '地锁状态',
            '电量：' + response.data.data.batteryCap + '%，地锁：' + lockSt
          )
        })
    },
    dataUp(lockCode, ty) {
      axios
        .get(
          'https://test.daodingtech.com/xdrpark-app/V1/nblot/lock?lockCode=' +
            lockCode +
            '&setingType=1&type=' +
            ty
        )
        .then(function(response) {
          if (response.data.errorCode != 200) {
            Toast('请求失败：' + response.data.errorMsg)
            return
          }
          Toast(response.data.errorMsg)
        })
    }
  }
}
</script>